package china.accountinghelper.model;

/**
 * 统计记录实体类，用来表示付款人和消费人之间的债务关系。不涉及数据库存储。
 * 
 * @author 王超
 */
public class ModelStatistics implements Comparable<ModelStatistics>
{
    private ModelPerson mPayer;
    private ModelPerson mConsumer;
    private long        mMoney;

    /**
     * 2个对象的付款人ID、消费人ID都相等的话，返回true；
     * 1个对象的付款人ID等于另1个对象的消费人ID、并且消费人ID等于付款人ID的话，返回true；
     * 其他返回false。
     * 
     */
    @Override
    public boolean equals(Object obj)
    {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;

        ModelStatistics other = (ModelStatistics) obj;
        
        if (mPayer.getID() == other.getPayer().getID() && mConsumer.getID() == other.getConsumer().getID()) {
            return true;
        } else if (mPayer.getID() == other.getConsumer().getID() && mConsumer.getID() == other.getPayer().getID()) {
            return true;
        } else {
            return false;
        }
        
    }

    /**
     * null排在后面；
     * 付款人是自己的排在前面；
     * 付款人ID不相等的话，付款人ID小的排在前面；
     * 付款人ID相等的话，消费人ID小的排在前面；
     */
    @Override
    public int compareTo(ModelStatistics another)
    {
        if (another == null) {
            return -1;
        }

        if (mPayer.getState() == ModelPerson.STATE_INDISPENSABLE && another.getPayer().getState() != ModelPerson.STATE_INDISPENSABLE) {
            return -1;
        } else if (mPayer.getState() != ModelPerson.STATE_INDISPENSABLE && another.getPayer().getState() == ModelPerson.STATE_INDISPENSABLE) {
            return 1;
        }

        if (mPayer.getID() != another.getPayer().getID()) {
            return (int) (mPayer.getID() - another.getPayer().getID());
        } else {
            return (int) (mConsumer.getID() - another.getConsumer().getID());
        }
    }

    public ModelPerson getPayer()
    {
        return mPayer;
    }

    public void setPayer(ModelPerson pPayer)
    {
        this.mPayer = pPayer;
    }

    public ModelPerson getConsumer()
    {
        return mConsumer;
    }

    public void setConsumer(ModelPerson pConsumer)
    {
        this.mConsumer = pConsumer;
    }

    public long getMoney1()
    {
        return mMoney;
    }

    public void setMoney(long pMoney)
    {
        this.mMoney = pMoney;
    }

    public double getMoney2()
    {
        return mMoney / 100.0;
    }

    public void setMoney(double pMoney)
    {
        mMoney = (long) pMoney * 100;
    }

}
